/*
 * Option.cpp
 *
 *  Created on: 06.05.2011
 *      Author: mifodix
 */

#include "Option.hpp"

Option::Option()
{
	data=NULL;
}

Option::Option(const Option &copy): code(copy.code), len(copy.len)
{
	this->data=new char[copy.len];
	memmove(this->data, copy.data, copy.len);
}

Option & Option::operator =(const Option & ob)
{
	if (this!=&ob)
	{
		this->code=ob.code;
		this->len=ob.len;
		if (this->data!=NULL)
		{
			delete[] data;
		}
		this->data=new char[ob.len];
		memmove(this->data, ob.data, len);
	}
	return *this;
}

Option::Option(unsigned char code, unsigned char len, const char *data): code(code)
{
	this->data=NULL;
	SetLenData(len, data);
}

unsigned char Option::GetCode() const
{
    return code;
}

unsigned char Option::GetLen() const
{
    return len;
}

char *Option::GetData() const
{
	char *data=new char[this->len];
	memmove(data, this->data, this->len);
	return data;
}

void Option::GetOption(unsigned char & code, unsigned char & len, char *data) const
{
	code=GetCode();
	len=GetLen();
	data=GetData();
}

void Option::SetCode(unsigned char code)
{
    this->code = code;
}

void Option::SetLen(unsigned char len)
{
    this->len = len;
}

void Option::SetLenData(unsigned char len, const char *data)
{
	SetLen(len);
	if (this->data!=NULL)
	{
		delete[] this->data;
	}
	this->data=new char[this->len];
	memmove(this->data, data, this->len);
}

size_t Option::GetSize() const
{
	return sizeof(code)+sizeof(len)+len;
}

bool Option::operator ==(const Option & other)
{
	if (this->code==other.code && this->len==other.len)
	{
		for (size_t i=0; i<this->len; i++)
		{
			if (!(this->data[i]==other.data[i])) return false;
		}
		return true;
	}
	return false;
}

void Option::SetOption(unsigned char code, unsigned char len, const char *data)
{
	SetCode(code);
	SetLenData(len, data);
}

Option::~Option()
{
	if (data!=NULL)
	{
		delete[] data;
	}
}
